<?php


namespace app\admin\controller;
use app\common\model\ArticleCate as AC;
use app\common\model\Category as C;

use app\common\model\GoodsCate as GC;
use app\common\model\PageCate as PC;
use think\facade\Request;
use think\facade\Route;
use think\facade\View;
use think\facade\Db;
class Category extends Base
{
    protected $validate = 'Category';
    public function index(){
        $pid = Request::param('pid'); //类别pid
        $where = Request::param('where', []);
        $where = array_filter($where);
        $keyword = Request::param('keyword');

        $list = C::where($where)
//            ->alias('c')
//            ->field('c.*,m.title,m.name,m.status as mstatus')
//            ->join('model m','c.moduleid = m.id')

            ->where(function ($query) use ($keyword) {
                if (!empty($keyword)) {
                    $query->whereLike('categoryname', "%$keyword%");
                }
            })
            ->select();
        //dump($list);die;
        $cate = category_tree($list);
        //dump($cate);die;

        $view = [
            'keyword' => $keyword,
            'list' => $cate,
            'header' =>Request::param('nav_type'),
            'pid' => $pid,

        ];
        View::assign($view);
        return View::fetch();
    }
    //添加
    public function page(){
        $page = PC::field('id as page_id,pagecatename,parentid')
            ->order('sort ASC,id ASC')
            ->select();
        $pagecate = page_tree($page);

        $view = [
            'pagecate'=>$pagecate,
            'type'=>'添加单页分类',
            'nav_type'=>Request::param('nav_type'),
            'data_type'=>Request::param('data_type'),
            'parentid' =>Request::param('parentid'),
            'info'=>null,
        ];
        View::assign($view);
        return View::fetch();
    }

    public function article(){
        $articate = AC::field('id as art_id,articlecatename,parentid')
            ->order('sort ASC,id ASC')
            ->select();
        $articate = articate_tree($articate);

        $view = [
            'articate'=>$articate,
            'type'=>'添加文章分类',
            'nav_type'=>Request::param('nav_type'),
            'data_type'=>Request::param('data_type'),
            'parentid' =>Request::param('parentid'),
            'info'=>null,
        ];
        View::assign($view);
        return View::fetch();
    }

    public function goods(){
        $goodscate = GC::field('id as gcate_id,goodscatename,image,parentid')
            ->order('sort ASC,id ASC')
            ->select();
        $goodscate = goodscate_tree($goodscate);

        $view = [
            'goodscate'=>$goodscate,
            'type'=>'添加商品分类',
            'nav_type'=>Request::param('nav_type'),
            'data_type'=>Request::param('data_type'),
            'parentid' =>Request::param('parentid'),
            'info'=>null,
        ];
        View::assign($view);
        return View::fetch();
    }
    public function addPost(){
        if(Request::isPost()){
            $data = Request::post();
            if (empty($data['nav_type'])){
                $this->error('请选择导航位置！');
            }
            $data['create_time'] = time();
            if (C::create($data)) {
                $this->success('添加成功!', 'index');
            } else {
                $this->error('添加失败');
            }
        }
    }
    //编辑
    public function pageedit(){
        $page = PC::field('id as page_id,pagecatename,parentid')
            ->order('sort ASC,id ASC')
            ->select();
        $pagecate = page_tree($page);
        $id = Request::param('id',0);
        $view = [

            'pagecate'=>$pagecate,
            'type'=>'编辑单页分类',
            'nav_type'=>Request::param('nav_type'),
            'data_type'=>Request::param('data_type'),
            'info'=>C::where('id',$id)->find(),
        ];
        View::assign($view);
        return View::fetch('page');
    }

    public function articleedit(){
        $articate = AC::field('id as art_id,articlecatename,parentid')
            ->order('sort ASC,id ASC')
            ->select();
        $articate = articate_tree($articate);
        $id = Request::param('id',0);
        $view = [
            'articate'=>$articate,
            'type'=>'编辑文章分类',
            'nav_type'=>Request::param('nav_type'),
            'data_type'=>Request::param('data_type'),
            'info'=>C::where('id',$id)->find(),
        ];
        View::assign($view);
        return View::fetch('article');
    }

    public function goodsedit(){
        $goodscate = GC::field('id as gcate_id,goodscatename,image,parentid')
            ->order('sort ASC,id ASC')
            ->select();
        $goodscate = goodscate_tree($goodscate);
        $id = Request::param('id',0);
        $view = [
            'goodscate'=>$goodscate,
            'type'=>'编辑商品分类',
            'nav_type'=>Request::param('nav_type'),
            'data_type'=>Request::param('data_type'),
            'info'=>C::where('id',$id)->find(),
        ];
        View::assign($view);
        return View::fetch('goods');
    }
    public function editPost(){
        if(Request::isPost()){
            $data = Request::post();
            if (empty($data['nav_type'])){
                $this->error('请选择导航位置！');
            }
            $data['create_time'] = time();
            $where['id'] = $data['id'];
            if (C::update($data,$where)) {
                $this->success('编辑成功!', 'index');
            } else {
                $this->error('编辑失败');
            }
        }
    }




    public function cate_del(){
        $get = Request::param('cate_id');
        //dump($get);die;
        $cateId = Db::name('category')->field(array('id','parentid'))->select();
        //dump($cateId);die;
        $delId = get_all_child($cateId,$get);
        //dump($delId);die;
        $delId[] = $get;
        $where = array(
            'id' => $delId,
        );
        foreach ($where as $k => $v){
            $categorypic = Db::name('category')->where(array('id'=>$v))->find();
        }


        //dump($categorypic);die;

        if(Db::name('category')->where($where)->delete()){
            return success('删除成功','index');
        }else{
            return error('删除失败！');
        }

    }

    public function del(){
        $id = Request::param('id','0');
        $ids = explode(',',$id);
        $cateId = C::field(array('id'))->select();
        //dump($cateId);die;
        foreach ($ids as $k =>$v){
            $delId = get_all_child($cateId,$v);
            $delId[] = $v;
            $where = array(
                'id' => $delId,
            );
            //dump($delId);die;
            $resuld = C::where($where)->delete();
        }
        if($resuld){
            return success('批量删除成功','index');
        }else{
            return error('批量删除失败！');
        }
    }
    public function ajaxCate(){
        $key = Request::param('name');
        $value = Request::param('param');
        $id = input('id', 0, 'intval');
        $cateadmin = new \app\common\model\Category();
        $res = $cateadmin->ajaxCateadmin($value, $id);
        save_log($key . ":查询数据成功！", 17);
        if ($res) {
            $data = array(
                'status' => 'n',
                'info' => '栏目名称已经重复，请一个试试'
            );
        } else {
            $data = array(
                'status' => 'y',
                'info' => '验证通过'
            );
        }
        return json($data);
    }



}